<template>
  <el-row class="login_container">
    <el-col :span="12" :offset="0"></el-col>
    <el-col :span="12" :offset="0">
      <el-form
        class="login_form"
        :model="loginForm"
        :rules="rules"
        ref="loginFormRef"
        status-icon
      >
        <h1>后台管理系统</h1>
        <el-form-item label="" prop="username">
          <el-input
            :prefix-icon="User"
            placeholder="用户名"
            v-model="loginForm.username"
          ></el-input>
        </el-form-item>
        <el-form-item label="" prop="password">
          <el-input
            :prefix-icon="Lock"
            placeholder="密码"
            v-model="loginForm.password"
            show-password
          ></el-input>
        </el-form-item>
        <el-row class="login_btn">
          <el-col :span="12">
            <el-form-item>
              <el-button :loading="loading" type="primary" @click="login()">
                登录
              </el-button>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item>
              <el-button type="warning">申请</el-button>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </el-col>
  </el-row>
</template>

<script setup lang="ts">
import { User, Lock } from '@element-plus/icons-vue' //icon图标 用户和锁
import { reactive, ref } from 'vue' //vue3响应式
import useUserStore from '@/store/modules/user' //引入用户仓库
import { ElNotification } from 'element-plus' //使用element消息提示框
import type { FormInstance } from 'element-plus' //引入类型
import { useRouter, useRoute } from 'vue-router' //使用vue路由跳转
import { getTime } from '@/utils/time'
let loginForm = reactive({ username: 'admin', password: 'atguigu123' }) //登录信息
let loading = ref(false) //加载图标
const userStore = useUserStore() //用户仓库
const loginFormRef = ref<FormInstance>()
const $router = useRouter()
const $route = useRoute()
//登录回调
const login = async () => {
  //检查表单校验是否通过
  await loginFormRef.value?.validate((value) => {
    if (!value) throw new Error('表单校验不通过')
    else console.log('submit')
  })
  loading.value = true //网速慢时显示加载符号
  try {
    await userStore.userLogin(loginForm) //用户登录
    if ($route.query.redirect) {
      $router.push('' + $route.query.redirect)
    } else $router.push('/')
    ElNotification({
      title: `Hi,${getTime()}好`,
      message: '登录成功',
      type: 'success',
    })
    loading.value = false
  } catch (error: any) {
    ElNotification({
      title: 'Error',
      message: error.message,
      type: 'error',
    })
    loading.value = false
  }
}
//校验用户名
const validateUserName = (_rule: any, value: any, callback: any) => {
  if (value === '') {
    callback(new Error('请输入用户名'))
  } else if (value.length < 5 || value.length > 20) {
    callback(new Error('用户名长度应在5-20之间'))
  } else callback()
}
//校验密码
const validatePassWord = (_rule: any, value: any, callback: any) => {
  if (value === '') {
    callback(new Error('请输入密码'))
  } else if (value.length < 5 || value.length > 20) {
    callback(new Error('密码长度应在5-20之间'))
  } else callback()
}
//表单校验对象
const rules = reactive({
  username: [{ validator: validateUserName, trigger: 'blur' }],
  password: [{ validator: validatePassWord, trigger: 'blur' }],
})
</script>

<style scoped lang="scss">
.login_container {
  width: 100%;
  height: 100vh;
  background: url('@/assets/images/background.jpg') no-repeat;
  background-size: cover;

  .login_form {
    position: relative;
    top: 30vh;
    width: 80%;
    background: url('@/assets/images/login_form.png') no-repeat;
    background-size: cover;
    padding: 40px;

    h1 {
      color: white;
      font-size: 29px;
      margin: 30px 0px;
    }

    .login_btn {
      width: 100%;
      position: relative;
      left: 20%;
    }
  }
}
</style>
